package com.diandian.backend.store.service;

import com.diandian.backend.store.constants.DianDianStoreConstant;
import com.diandian.backend.store.domain.StoreMeituanShangouStoreTree;
import com.diandian.backend.store.dto.*;
import com.diandian.backend.store.mapper.StoreMeituanShangouStoreTreeMapper;
import com.diandian.backend.store.resp.StoreMeituanShangouStoreTreeDetailResp;
import com.diandian.backend.store.resp.StoreMeituanShangouStoreTreeListResp;
import com.diandian.backend.store.resp.StoreMeituanShangouStoreTreePageResp;
import com.diandian.base.boot.starter.exception.DefaultException;
import com.diandian.base.boot.starter.response.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class StoreMeituanShangouStoreTreeService {

    @Autowired
    private StoreMeituanShangouStoreTreeMapper storeMeituanShangouStoreTreeMapper;

    public PageResult page(StoreMeituanShangouStoreTreePageParamDTO requestDTO){

        requestDTO.check();
        int pageNum = requestDTO.getPageNum();
        int pageSize = requestDTO.getPageSize();

        // 查询总数
        int totalCount = storeMeituanShangouStoreTreeMapper.getCount(requestDTO);

        // 查询数据集合
        List<StoreMeituanShangouStoreTree> dataList = storeMeituanShangouStoreTreeMapper.getPage(requestDTO, requestDTO.getPageSize(), (requestDTO.getPageNum() - 1) * requestDTO.getPageSize());

        // 组装数据
        List<StoreMeituanShangouStoreTreePageResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){
            dataList.stream().forEach( x ->{
                StoreMeituanShangouStoreTreePageResp item = new StoreMeituanShangouStoreTreePageResp();
                item.setId(x.getId());
                item.setType(x.getType());
                item.setNodeCode(x.getNodeCode());
                item.setNodeName(x.getNodeName());
                item.setNodePath(x.getNodePath());
                item.setParentNodeCode(x.getParentNodeCode());
                item.setRemark(x.getRemark());
                item.setBelong(x.getBelong());
                item.setCreateUserId(x.getCreateUserId());
                item.setCreateUserName(x.getCreateUserName());
                item.setCreateTime(x.getCreateTime());
                item.setOrgId(x.getOrgId());
                item.setOrgCode(x.getOrgCode());
                item.setUpdateUserId(x.getUpdateUserId());
                item.setUpdateUserName(x.getUpdateUserName());
                item.setUpdateTime(x.getUpdateTime());
                item.setDel(x.getDel());
             respList.add(item);
            });
        }

        return new PageResult(pageNum, pageSize, totalCount, respList);
    }

    public List<StoreMeituanShangouStoreTreeListResp> getList(StoreMeituanShangouStoreTreeListParamDTO requestDTO){

        // 查询数据集合
        List<StoreMeituanShangouStoreTree> dataList = storeMeituanShangouStoreTreeMapper.getList(requestDTO);

        // 组装数据
        List<StoreMeituanShangouStoreTreeListResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){
            dataList.stream().forEach( x ->{
                StoreMeituanShangouStoreTreeListResp item = new StoreMeituanShangouStoreTreeListResp();
                item.setId(x.getId());
                item.setType(x.getType());
                item.setNodeCode(x.getNodeCode());
                item.setNodeName(x.getNodeName());
                item.setNodePath(x.getNodePath());
                item.setNodeLevel(x.getNodeLevel());
                item.setParentNodeCode(x.getParentNodeCode());
                item.setRemark(x.getRemark());
                item.setBelong(x.getBelong());
                item.setCreateUserId(x.getCreateUserId());
                item.setCreateUserName(x.getCreateUserName());
                item.setCreateTime(x.getCreateTime());
                item.setOrgId(x.getOrgId());
                item.setOrgCode(x.getOrgCode());
                item.setUpdateUserId(x.getUpdateUserId());
                item.setUpdateUserName(x.getUpdateUserName());
                item.setUpdateTime(x.getUpdateTime());
                item.setDel(x.getDel());
                respList.add(item);
            });
        }

        return respList;
    }

    public StoreMeituanShangouStoreTreeDetailResp getDetail(StoreMeituanShangouStoreTreeDetailParamDTO param) {

        StoreMeituanShangouStoreTree dbData = storeMeituanShangouStoreTreeMapper.getDetail(param);
        if(dbData == null){
            return null;
        }

        StoreMeituanShangouStoreTreeDetailResp result = new StoreMeituanShangouStoreTreeDetailResp();
        result.setId(dbData.getId());
        result.setType(dbData.getType());
        result.setNodeCode(dbData.getNodeCode());
        result.setNodeName(dbData.getNodeName());
        result.setNodePath(dbData.getNodePath());
        result.setNodeLevel(dbData.getNodeLevel());
        result.setParentNodeCode(dbData.getParentNodeCode());

        String parentNodeCode = dbData.getParentNodeCode();
        if(DianDianStoreConstant.TREE_NODE_CODE_TOP.equals(parentNodeCode)){
            result.setParentNodeCode(DianDianStoreConstant.TREE_NODE_CODE_TOP);
            result.setParentNodeName(DianDianStoreConstant.TREE_NODE_NAME_TOP);
        } else {
            StoreMeituanShangouStoreTreeDetailParamDTO parentParam = new StoreMeituanShangouStoreTreeDetailParamDTO();
            parentParam.setNodeCode(dbData.getParentNodeCode());
            parentParam.setBelong(dbData.getBelong());
            parentParam.setOrgId(dbData.getOrgId());
            parentParam.setOrgCode(dbData.getOrgCode());
            StoreMeituanShangouStoreTree parentData = storeMeituanShangouStoreTreeMapper.getDetail(parentParam);
            result.setParentNodeName((parentData == null || parentData.getNodeName() == null) ? "" : parentData.getNodeName());
        }

        result.setRemark(dbData.getRemark());
        result.setBelong(dbData.getBelong());
        result.setCreateUserId(dbData.getCreateUserId());
        result.setCreateUserName(dbData.getCreateUserName());
        result.setCreateTime(dbData.getCreateTime());
        result.setOrgId(dbData.getOrgId());
        result.setOrgCode(dbData.getOrgCode());
        result.setUpdateUserId(dbData.getUpdateUserId());
        result.setUpdateUserName(dbData.getUpdateUserName());
        result.setUpdateTime(dbData.getUpdateTime());
        result.setDel(dbData.getDel());
        return result;
    }

    public void updateById(StoreMeituanShangouStoreTreeUpdateByIdParamDTO param) {
        param.check();

        StoreMeituanShangouStoreTreeDetailParamDTO queryDetailDTO = new StoreMeituanShangouStoreTreeDetailParamDTO();
        queryDetailDTO.setId(param.getId());
        StoreMeituanShangouStoreTree dbData = storeMeituanShangouStoreTreeMapper.getDetail(queryDetailDTO);
        if(dbData == null){
            throw new DefaultException("更新的记录不存在");
        }

        storeMeituanShangouStoreTreeMapper.updateById(param);
    }

    public void updateNodePath(Long id, String nodePath) {
        storeMeituanShangouStoreTreeMapper.updateNodePath(id, nodePath);
    }

    public void updateParentInfo(Long id, String parentNodeCode, String nodePath, int nodeLevel) {
        storeMeituanShangouStoreTreeMapper.updateParentInfo(id, parentNodeCode, nodePath, nodeLevel);
    }

    public void updateNextPathInfo(List<StoreMeituanShangouStoreTreeUpdateByIdParamDTO> nextNodeUpdateList){
        storeMeituanShangouStoreTreeMapper.updateNextPathInfo(nextNodeUpdateList);
    }

    public void deleteById(StoreMeituanShangouStoreTreeDeleteByIdParamDTO paramDTO) {
        paramDTO.check();
        storeMeituanShangouStoreTreeMapper.deleteById(paramDTO);
    }

    public void add(StoreMeituanShangouStoreTreeAddParamDTO param) {
        param.setDel(0);
        param.setCreateTime(new Date());
        storeMeituanShangouStoreTreeMapper.add(param);
    }
}
